#include "NonFinAutToRegGrammatica.h"

RegGrammatica<string> NonFinAutToRegGrammatica::nonFinAutToRegGrammatica(NonFinAut<string> &nonFinAut)
{
	//converteren volgens stelling 3.2b
	list<char> symbols = nonFinAut.getSymbolList();
	list<string> states = nonFinAut.getStateList();
	list<Transition<string> > transitionList = nonFinAut.getTransitionList();
	list<string> startStates = nonFinAut.getStartList();
	list<ProductionRule<string> > productionRules;
	for(list<string>::iterator s = states.begin(); s != states.end(); s++)
	{
		string fromState = *s;
		for(list<char>::iterator sym = symbols.begin(); sym != symbols.end(); sym++)
		{
			char symbol = *sym;
			list<Transition<string> > theStates;
			for(list<Transition<string> >::iterator t = transitionList.begin();t != transitionList.end(); t++)
			{
				if(t->begin == fromState && t->symbol == symbol)
				{
					theStates.push_back(*t);
				}
			}
			for(list<Transition<string> >::iterator t = theStates.begin(); t != theStates.end(); t++)
			{
				string toState = t->end;
				string fromStateRewrited = string("A") + fromState[1];
				string toStateRewrited = string("A") + toState[1];
				if(nonFinAut.isEndState(toState))
				{
					ProductionRule<string> productionRuleEnd = ProductionRule<string>(fromStateRewrited, symbol);
					productionRules.push_back(productionRuleEnd);
				}
				
				ProductionRule<string> productionRule = ProductionRule<string>(fromStateRewrited, symbol, toStateRewrited );
				productionRules.push_back(productionRule);
				
			}
		}
	}
	string startRule = *startStates.begin();
	string startRuleRewrited = string("A") + startRule[1];

	RegGrammatica<string> result = *new RegGrammatica<string>(productionRules, startRuleRewrited);
	return result;
}